package com.pzhyd.sc.biz.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.pzhyd.sc.util.HibernateUtil;
import com.pzhyd.sc.biz.inter.IvoteBiz;
import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.entity.Roleinfo;
import com.pzhyd.sc.entity.Typeinfo;
import com.pzhyd.sc.entity.Votedinfo;
import com.pzhyd.sc.entity.Voteoption;
import com.pzhyd.sc.util.Page;

/** 
 * @Project: webvote
 * @Title: VoteBizImpl.java
 * @Package com.pzhyd.sc.biz.impl
 * @Description: TODO
 * @author Gaoshiwei 812257531@qq.com
 * @date Jul 13, 2012 9:12:31 AM
 * @Copyright: 2012 攀枝花移动 All rights reserved.
 * @version V1.0  
 */
public class VoteBizImpl implements IvoteBiz {
	
	private IBaseDao idao;
	
	public IBaseDao getIdao() {
		return idao;
	}

	public void setIdao(IBaseDao idao) {
		this.idao = idao;
	}

	public boolean deleteVoteoption(Voteoption vo) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.delete(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean deleteVoteoptions(int[] vo_id) {
		// TODO Auto-generated method stub
		int count=0;
		StringBuilder sbStr = new StringBuilder();
		for (int i=0; i<vo_id.length; i++) {
			sbStr.append("?");
			if (i < (vo_id.length - 1)) {
				sbStr.append(",");
			}
		}
		List params = new ArrayList();
		for (int i=0; i<vo_id.length; i++) {
			params.add(vo_id[i]);
		}
		
		 String hql = "delete from Voteoption where voId in (" + sbStr.toString()  +  ")";
		 try {
			count=idao.operateHql(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(count>0)
			return true;
		else
			return false;
	}

	public boolean deleteVoter(Votedinfo vi) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.delete(vi);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean deleteVoters(int[] viid) {
		// TODO Auto-generated method stub
		int count=0;
		StringBuilder sbStr = new StringBuilder();
		for (int i=0; i<viid.length; i++) {
			sbStr.append("?");
			if (i < (viid.length - 1)) {
				sbStr.append(",");
			}
		}
		List params = new ArrayList();
		for (int i=0; i<viid.length; i++) {
			params.add(viid[i]);
		}
		
		 String hql = "delete from Votedinfo where viId in (" + sbStr.toString()  +  ")";
		 try {
			count=idao.operateHql(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(count>0)
			return true;
		else
			return false;
	}

	public boolean insertVoteoption(Voteoption vo) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.insert(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean insertVoter(Votedinfo vi) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.insert(vi);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean updateVoteoption(Voteoption vo) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.update(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public boolean updateVoter(Votedinfo vi) {
		// TODO Auto-generated method stub
		int count = 0;
		boolean flag=false;
		try {
			count = idao.update(vi);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(count>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	public void searchAllvoteoptions(Page page) {
		// TODO Auto-generated method stub
		String hql1 = "select count(*) from Voteoption";
		String hql = "from Voteoption vo order by vo.voId";
		try {
			page.setTotalRecords(idao.getTotalCount(hql1, null));
			idao.searchHql(hql, null, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void searchAllvoter(Page page) {
		// TODO Auto-generated method stub
		String hql1 = "select count(*) from Votedinfo";
		String hql = "from Votedinfo vi order by vi.viTimes desc";
		try {
			page.setTotalRecords(idao.getTotalCount(hql1, null));
			idao.searchHql(hql, null, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Votedinfo searchByid(int viid) {
		// TODO Auto-generated method stub
		Votedinfo vi=null;
		try {
			vi=(Votedinfo) idao.searchById(Votedinfo.class, viid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return vi;
	}

	public List<Votedinfo> searchAllvoter() {
		// TODO Auto-generated method stub
		String hql = "from Votedinfo v order by v.viTimes desc";
		List<Votedinfo> lst = null;
		try {
			lst = idao.searchHql(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

	public Voteoption searchByIP(String ip) {
		// TODO Auto-generated method stub
		String hql = "from Voteoption v where v.voIpaddr=? order by voTime desc";
		List parms = new ArrayList();
		parms.add(ip);
		List<Voteoption>  lv= null;
		try {
			lv= idao.searchHql(hql, parms);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(lv!=null){
			return lv.get(0);
		}
		else
			return null;
	}

	public boolean vote(Votedinfo vi, Voteoption vo) {
		// TODO Auto-generated method stub
		boolean flag=false;
		try {
			Session ses = HibernateUtil.getThreadLocalSession();
			ses.beginTransaction();
			ses.save(vo);
			ses.update(vi);
			ses.getTransaction().commit();
			HibernateUtil.closeThreadLocalSession();
			flag=true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			return flag;
		}
	}

}
